{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2162b9f1",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/data_connectors/WeaviateDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "36e7bb96-0c27-47e9-a525-c11f40be3b86",
   "metadata": {},
   "source": [
    "# Weaviate Reader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a235ac8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-readers-weaviate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38ca1434",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4d1da511",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ec37a7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d99bc57b-85df-46ac-8262-2409344af428",
   "metadata": {},
   "outputs": [],
   "source": [
    "import weaviate\n",
    "from llama_index.readers.weaviate import WeaviateReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fec36c7a-3766-4167-890e-b93adb831a64",
   "metadata": {},
   "outputs": [],
   "source": [
    "# See https://weaviate.io/developers/weaviate/current/client-libraries/python.html\n",
    "# for more details on authentication\n",
    "resource_owner_config = weaviate.AuthClientPassword(\n",
    "    username=\"<username>\",\n",
    "    password=\"<password>\",\n",
    ")\n",
    "\n",
    "# initialize reader\n",
    "reader = WeaviateReader(\n",
    "    \"https://<cluster-id>.semi.network/\",\n",
    "    auth_client_secret=resource_owner_config,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce9f299c-4f0a-4bca-bc90-79848f02b381",
   "metadata": {},
   "source": [
    "You have two options for the Weaviate reader: 1) directly specify the class_name and properties, or 2) input the raw graphql_query. Examples are shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b92d69a1-d39f-45cf-a136-cb9c2f2f5cdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1) load data using class_name and properties\n",
    "# docs = reader.load_data(\n",
    "#    class_name=\"Author\", properties=[\"name\", \"description\"], separate_documents=True\n",
    "# )\n",
    "\n",
    "documents = reader.load_data(\n",
    "    class_name=\"<class_name>\",\n",
    "    properties=[\"property1\", \"property2\", \"...\"],\n",
    "    separate_documents=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "722b5d47-9897-4c54-9734-259ab0c1634c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2) example GraphQL query\n",
    "# query = \"\"\"\n",
    "# {\n",
    "#   Get {\n",
    "#     Author {\n",
    "#       name\n",
    "#       description\n",
    "#     }\n",
    "#   }\n",
    "# }\n",
    "# \"\"\"\n",
    "# docs = reader.load_data(graphql_query=query, separate_documents=True)\n",
    "\n",
    "query = \"\"\"\n",
    "{\n",
    "  Get {\n",
    "    <class_name> {\n",
    "      <property1>\n",
    "      <property2>\n",
    "      ...\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "documents = reader.load_data(graphql_query=query, separate_documents=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "169b4273-eb20-4d06-9ffe-71320f4570f6",
   "metadata": {},
   "source": [
    "### Create index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92599a0a-93ba-4c93-80f1-9acae0663c34",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = SummaryIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52d93c3f-a08d-4637-98bc-0c3cc693c563",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"<query_text>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "771b42be-4108-43a0-a1b4-b259a7819936",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
